﻿using BLL;
using LibraryMIS2019.Properties;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LibraryMIS2019
{
    public partial class LibraryUI : Form
    {
        private string operation = "I";     //图书操作类型变量，当为I时是新增书籍，当为U时是更新书籍信息
        BusinessLogic logic = new BusinessLogic();  //新建业务逻辑类对象logic
        DataSet ds; //定义数据集对象
        public LibraryUI()
        {
            InitializeComponent();
            //控件透明化处理
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            labUserName.BackColor = Color.FromArgb(0, 255, 255, 255);//UI窗口下方Label字体透明化处理
            labLoginUser.BackColor = Color.FromArgb(0, 255, 255, 255);//UI窗口下方Label字体透明化处理
        }

        #region 标签颜色渲染处理
        /// <summary>
        /// 修改TabPage标签的颜色
        /// 1.将TabControl控件DrawMode属性修改为：OwnerDrawFixed
        /// 2.通过编辑TabControl控件的DrawItem事件进行本事件的编程实现
        /// </summary>
        private void tabControlLib_DrawItem(object sender, DrawItemEventArgs e)
        {
            //获取TabControl主控件的工作区域
            Rectangle rec = tabControlLib.ClientRectangle;
            //获取背景图片，我的背景图片在项目资源文件中。
            Image backImage = Resources.background;

            //新建一个StringFormat对象，用于对标签文字的布局设置
            StringFormat StrFormat = new StringFormat();
            StrFormat.LineAlignment = StringAlignment.Center;// 设置文字垂直方向居中
            StrFormat.Alignment = StringAlignment.Center;// 设置文字水平方向居中           

            // 标签背景填充颜色：#EEFFFF(238, 255, 255)，#FFFFEE(255, 255, 238)
            //当然也可以设置不同颜色值
            //SolidBrush blue1 = new SolidBrush(Color.FromArgb(238, 255, 255));
            SolidBrush color1 = new SolidBrush(Color.FromArgb(238, 255, 255));
            SolidBrush color2 = new SolidBrush(Color.FromArgb(255, 255, 238));
            SolidBrush color3 = new SolidBrush(Color.FromArgb(238, 255, 255));
            SolidBrush color4 = new SolidBrush(Color.FromArgb(255, 255, 238));
            SolidBrush color5 = new SolidBrush(Color.FromArgb(238, 255, 255));
            SolidBrush color6 = new SolidBrush(Color.FromArgb(255, 255, 238));
            SolidBrush color7 = new SolidBrush(Color.FromArgb(238, 255, 255));
            SolidBrush bruFont = new SolidBrush(Color.FromArgb(0, 34, 85));// 标签字体颜色
            Font fontStyle = new System.Drawing.Font("微软雅黑", 10F);//设置标签字体样式 
            //绘制主控件的背景
            e.Graphics.DrawImage(backImage, 0, 0, tabControlLib.Width, tabControlLib.Height);
            //绘制背景色
            Rectangle rec1 = tabControlLib.GetTabRect(0);
            e.Graphics.FillRectangle(color1, rec1);
            Rectangle rec2 = tabControlLib.GetTabRect(1);
            e.Graphics.FillRectangle(color2, rec2);
            Rectangle rec3 = tabControlLib.GetTabRect(2);
            e.Graphics.FillRectangle(color3, rec3);
            Rectangle rec4 = tabControlLib.GetTabRect(3);
            e.Graphics.FillRectangle(color4, rec4);
            Rectangle rec5 = tabControlLib.GetTabRect(4);
            e.Graphics.FillRectangle(color5, rec5);
            Rectangle rec6 = tabControlLib.GetTabRect(5);
            e.Graphics.FillRectangle(color6, rec6);
            Rectangle rec7 = tabControlLib.GetTabRect(6);
            e.Graphics.FillRectangle(color7, rec7);
            //绘制标签样式
            for (int i = 0; i < tabControlLib.TabPages.Count; i++)
            {
                //获取标签头的工作区域
                Rectangle recChild = tabControlLib.GetTabRect(i);
                //绘制标签头背景颜色
                // e.Graphics.FillRectangle(blue1, recChild);
                //绘制标签头的文字                        
                e.Graphics.DrawString(tabControlLib.TabPages[i].Text, fontStyle, bruFont, recChild, StrFormat);
            }
        }
        #endregion

        #region 图书类别下拉实现
        /// <summary>
        /// 下拉菜单方法实现
        /// </summary>
        private void DropDownBookClass(ComboBox controlName)
        {
            DataSet bookClass = logic.GetBookClass();
            //数据源绑定到控件
            controlName.DataSource = bookClass.Tables[0];
            controlName.DisplayMember = "classname";
            controlName.ValueMember = "classid";
        }
        private void tag1cbBookCategory1_VisibleChanged(object sender, EventArgs e)
        {
            try
            {
                DropDownBookClass(tag1cbBookCategory1);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统信息");
            }
        }

        private void tag1cbBookCategory2_VisibleChanged(object sender, EventArgs e)
        {
            try
            {
                DropDownBookClass(tag1cbBookCategory2);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "系统信息");
            }
        }
        #endregion

        #region 图书信息处理
        /// <summary>
        /// 书籍入库数据校验
        /// </summary>
        private string BookInfoDataValidation()
        {
            if (tag1txtBookID.Text == "") { tag1txtBookID.Focus(); return "索书号不能为空值！"; }
            if (tag1txtBookName1.Text == "") { tag1txtBookName1.Focus(); return "书名不能为空值！"; }
            if (tag1txtISBN.Text == "") { tag1txtISBN.Focus(); return "图书标准编号不能为空值！"; }
            if (tag1txtAuthor.Text == "") { tag1txtAuthor.Focus(); return "作者不能为空值！"; }
            if (tag1txtPublisher.Text == "") { tag1txtPublisher.Focus(); return "出版社不能为空值！"; }
            return "";
        }
        /// <summary>
        /// 图书新增/更新操作单选按钮
        /// </summary>
        private void tag1btnInsertBook_Click(object sender, EventArgs e)
        {
            string errorString = BookInfoDataValidation();
            if (errorString.Length > 0) { MessageBox.Show(errorString, "系统提示"); return;  }
            //获取控件值
            BookInfo bo = new BookInfo();
            bo.BookID = tag1txtBookID.Text.Trim();
            bo.BookName = tag1txtBookName1.Text.Trim();
            bo.ISBN = tag1txtISBN.Text.Trim();
            bo.Author = tag1txtAuthor.Text.Trim();
            bo.Publisher = tag1txtPublisher.Text.Trim();
            if (tag1txtWordCount.Text == "")
                bo.WordCount = 0;
            else bo.WordCount = Convert.ToInt32(tag1txtWordCount.Text.Trim());
            if (tag1txtPageCount.Text == "")
                bo.WordCount = 0;
            else bo.WordCount = Convert.ToInt32(tag1txtPageCount.Text.Trim());
            //日期控件取值处理
            bo.PublishDate = tag1dtpPublishDate.Value.Date;

            if (tag1txtVersion.Text.Trim().Length>0)
                bo.BookVersion = tag1txtVersion.Text.Trim();
            else bo.BookVersion = "";

            bo.BookClass.BookClassID = tag1cbBookCategory1.SelectedValue.ToString().Trim();

            logic = new BusinessLogic();
            object obj = logic.EditBook(bo, operation);
        }


        #endregion

    }
}
